package 文件;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class 计算器 extends JFrame implements ActionListener{
   // private JButton btn;
    private JTextField tf;//用来显示计算结果
    private JButton clrbtn;//清除按钮
    public 计算器(){
        setTitle("计算器");
        setVisible(true);
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       /* btn=new JButton("确定");
        add(btn, BorderLayout.EAST);*/
       JPanel panel1=new JPanel();
       JPanel panel2=new JPanel();
       tf=new JTextField();
       panel1.setLayout(new BorderLayout());
       panel1.add(tf);
       clrbtn =new JButton("清除");
       panel1.add(clrbtn,BorderLayout.EAST);
       add(panel1,BorderLayout.NORTH);
       panel2.setLayout(new GridLayout(4,4));
       String[] str={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
       for (String s:str){
           JButton bt=new JButton(s);
           bt.addActionListener(this);
           panel2.add(bt);
       }
      add(panel2);

   }

   @Override
    public void actionPerformed(ActionEvent e) {
        String s=e.getActionCommand();
        tf.setText(tf.getText()+s);
        //JOptionPane.showMessageDialog(this,s);
    }

    public static void main(String[]args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new 计算器();
            }
        });

    }

}
